package leetcode101.ch3;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

//LC 159
public class LengthOfLongestSubstringTwoDistinct2 {
    public int lengthOfLongestSubstringTwoDistinct(String s) {
        if (s.length() <= 2) {
            return s.length();
        }
        int[] map = new int[128];
        int count = 0;
        int length = 0;
        for (int l = 0, r = 0; r < s.length();) {
            if (count <= 2){
                if (map[s.charAt(r++)]++ == 0) {
                    count++;
                }
            }else if (--map[s.charAt(l++)] == 0) {
                count--;
            }
            if(count <=2) {
                length = Math.max(length, r - l);
            }
        }
        return length;
    }

    public static void main(String[] args) {
        new LengthOfLongestSubstringTwoDistinct2().lengthOfLongestSubstringTwoDistinct("aaaaaaaaaa");
    }


}
